package com.example.androidad;

import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtil {
    public static void postSync(){//同步请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient=new OkHttpClient();
                FormBody formBody=new FormBody.Builder()
                        .add("fromplatform","1")
                        .add("appid",AdUtil.appid)
                        .add("lan",AdUtil.lan)
                        .add("secretkey",AdUtil.appsecret)
                        .build();
                Request request=new Request.Builder()
                        .post(formBody)
                        .url("https://www.hpytg.com/requestadapi")
                        .build();
                //准备好请求的Call对象
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    Log.i("postSync",response.body().string());
                    Gson gson = new Gson();
                    Responsedata adModel = gson.fromJson(response.body().string(), Responsedata.class);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public static void postAsync(){//异步请求
        OkHttpClient okHttpClient=new OkHttpClient();
        FormBody formBody=new FormBody.Builder()
                .add("a","1")
                .add("b","2")
                .build();
        Request request=new Request.Builder()
                .post(formBody)
                .url("https://www.httpbin.org/post")
                .build();
        //准备好请求的Call对象
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if(response.isSuccessful()){
                    Log.i("postAsync",response.body().string());
                }
            }
        });
    }
    public static String postrequest() throws JSONException {
        OkHttpClient client = new OkHttpClient();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fromplatform", "1");
        jsonObject.put("appid", AdUtil.appid);
        jsonObject.put("secretkey", AdUtil.appsecret);
        jsonObject.put("lan", AdUtil.lan);
//  .add("fromplatform","1")
//                .add("appid",AdUtil.appid)
//                .add("lan",AdUtil.lan)
//                .add("secretkey",AdUtil.appsecret)
        RequestBody requestJsonBody = RequestBody.create(
                jsonObject.toString(),
                MediaType.parse("application/json")
        );

        Request postRequest = new Request.Builder()
                .url("https://www.hpytg.com/requestadapi")
                .post(requestJsonBody)
                .build();

        try {
            Response response = client.newCall(postRequest).execute();
            String data = response.body().toString();
            System.out.println(response.body().string());
            return  data;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }
}